package com.gcd.skymallorderapi.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.Order;
import com.situ.model.search.OrderSearchBean;
import org.apache.ibatis.annotations.Mapper;

import java.util.Objects;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    // 分页查询订单
    default Page<Order> findAll(Page<Order> page, OrderSearchBean searchBean) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();

        if (searchBean != null) {
            // 处理Order实体中存在的字段
            queryWrapper.eq(Objects.nonNull(searchBean.getId()), Order::getId, searchBean.getId())
                    .eq(Objects.nonNull(searchBean.getUserId()), Order::getUserId, searchBean.getUserId())
                    .eq(Objects.nonNull(searchBean.getOrderStatus()), Order::getOrderStatus, searchBean.getOrderStatus())
                    .eq(Objects.nonNull(searchBean.getPayStatus()), Order::getPayStatus, searchBean.getPayStatus())
                    .eq(Objects.nonNull(searchBean.getOrderType()), Order::getOrderType, searchBean.getOrderType())
                    .like(Objects.nonNull(searchBean.getOrderNo()), Order::getOrderNo, searchBean.getOrderNo());
            // 处理时间范围查询条件
            if (searchBean.getStartTimeForPay() != null) {
                queryWrapper.ge(Order::getPaymentTime, searchBean.getStartTimeForPay());
            }
            if (searchBean.getEndTimeForPay() != null) {
                queryWrapper.le(Order::getPaymentTime, searchBean.getEndTimeForPay());
            }
            if (searchBean.getStartTimeForShipping() != null) {
                queryWrapper.ge(Order::getShippingTime, searchBean.getStartTimeForShipping());
            }
            if (searchBean.getEndTimeForShipping() != null) {
                queryWrapper.le(Order::getShippingTime, searchBean.getEndTimeForShipping());
            }
        }
        return selectPage(page, queryWrapper);
    }

    // 根据订单id查询订单项
    Page<Order> findByOrderId(Page<Order> page, Integer id);

    //根据秒杀编号查询订单信息
    default Order findBySeckillNoOrder(String seckillNo) {
        LambdaQueryWrapper<Order> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(Order::getSeckillNo,seckillNo);
        return selectOne(queryWrapper);
    }

    //根据id查询订单信息
    default Order findById(Integer id) {
        LambdaQueryWrapper<Order> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(Order::getId,id);
        return selectOne(queryWrapper);
    }



    //前台点击支付按钮，将支付状态和订单状态改为已支付（1）
    int updatePayStatusInt(Integer  id);

    //后台点击发货，将发货状态改为已发货（2）
    int updateSendStatusIntTo2(Integer  id);

    //后台点击收货，将收货状态改为已收货（3）
    int updateReceiveStatusIntTo3(Integer  id);

    //12小时后未付款的订单改为取消（4）
    int updateStatusTo4(Integer  id);

    // 12小时后未发货的订单改为取消（5）
    int updateStatusTo5(Integer  id);

    //退款成功，订单状态改为退款成功（6）
    int updateStatusTo6(Integer  id);
}
